//
//  LYEmohTopView.swift
//  LYOnlineBanking
//
//  Created by 李永彬 on 2025/7/21.
//

import UIKit

class LYEmohTopView: UIControl {
    
    lazy var bgImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "home_background_top")
        return imageView
    }()
    
    lazy var maximumTitleLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex: "#FFFFFF")
        label.font = UIFont.systemFont(ofSize: 12, weight: .bold)
//        label.text = "Maximum amount"
        return label
    }()
    
    lazy var maximumAmountLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex: "#FFFFFF")
        label.font = UIFont.systemFont(ofSize: 38, weight: .bold)
//        label.text = "9300000"
        label.adjustsFontSizeToFitWidth = true
        return label
    }()
    
    let daysView = LYEmohDaysView()
    
    lazy var applyButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setBackgroundImage(UIImage(named: "home_apply_button"), for: .normal)
        button.addTarget(self, action: #selector(applyButtonAction(_:)), for: .touchUpInside)
        button.isUserInteractionEnabled = false
        return button
    }()
    
    lazy var applyTitleLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex: "#FFFFFF")
        label.font = UIFont.systemFont(ofSize: 16, weight: .bold)
        label.adjustsFontSizeToFitWidth = true
        return label
    }()
    
    lazy var tipsImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "home_cause")
        return imageView
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(bgImageView)
        addSubview(maximumTitleLabel)
        addSubview(maximumAmountLabel)
        addSubview(daysView)
        addSubview(applyButton)
        applyButton.addSubview(tipsImageView)
        applyButton.addSubview(applyTitleLabel)
        commonInitSubviews()
    }
    
    func commonInitSubviews() {
                
        bgImageView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        maximumTitleLabel.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(16)
            make.top.equalToSuperview().offset(30)
        }
        
        maximumAmountLabel.snp.makeConstraints { make in
            make.leading.equalTo(maximumTitleLabel).offset(16)
            make.trailing.equalTo(daysView.snp.leading).offset(-5)
            make.top.equalTo(maximumTitleLabel).offset(20)
        }
        
        daysView.snp.makeConstraints { make in
            make.trailing.equalToSuperview().offset(-52)
            make.top.equalTo(maximumTitleLabel.snp.bottom)
            make.height.equalTo(54)
        }
        
        applyButton.snp.makeConstraints { make in
            make.bottom.equalToSuperview().offset(-25)
            make.centerX.equalToSuperview().offset(-20)
            make.height.equalTo(51)
        }
        
        tipsImageView.snp.makeConstraints { make in
            make.trailing.equalTo(applyButton).offset(-30)
            make.top.equalTo(applyButton.snp.centerY).offset(-10)
            make.bottom.equalTo(applyButton)
        }
        
        applyTitleLabel.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(10)
            make.centerY.equalToSuperview()
            make.trailing.lessThanOrEqualTo(tipsImageView.snp.leading).offset(-5)
        }
    }
    
    func updateDatas(maximumTitle: String,
                     maximumAmount: String,
                     deadLine: String,
                     rate: String,
                     applyTitle: String) {
        self.maximumTitleLabel.text = maximumTitle
        self.maximumAmountLabel.text = maximumAmount
        self.applyTitleLabel.text = applyTitle
        daysView.updateDatas(deadLine: deadLine, rate: rate)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func applyButtonAction(_ button: UIButton) {
        
    }
}

class LYEmohDaysView: UIView {
    
    lazy var bgImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "home_days_background")
        return imageView
    }()
    
    lazy var deadLineLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex: "#000000")
        label.font = UIFont.systemFont(ofSize: 14, weight: .bold)
        label.adjustsFontSizeToFitWidth = true
        return label
    }()
    
    lazy var rateLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex: "#000000")
        label.font = UIFont.systemFont(ofSize: 14, weight: .bold)
        label.adjustsFontSizeToFitWidth = true
        return label
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(bgImageView)
        addSubview(deadLineLabel)
        addSubview(rateLabel)
        commonInitSubviews()
    }
    
    func commonInitSubviews() {
                
        bgImageView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        deadLineLabel.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(15)
            make.trailing.equalToSuperview().offset(-10)
            make.top.equalToSuperview().offset(12)
        }
        
        rateLabel.snp.makeConstraints { make in
            make.leading.trailing.equalTo(deadLineLabel)
            make.bottom.equalToSuperview().offset(-8)
        }
    }
    
    func updateDatas(deadLine: String,
                     rate: String) {
        self.deadLineLabel.text = deadLine
        self.rateLabel.text = rate
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
